package com.zbkj.crmeb.store.model;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.zbkj.crmeb.enums.task.TaskTypeEnums;
import com.zbkj.crmeb.store.FeatureBaseDO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("eb_task")
@ApiModel(value="task对象", description="任务表")
public class Task extends FeatureBaseDO implements Serializable {
    private static final long serialVersionUID=11234422167856L;

    @ApiModelProperty(value = "主键ID")
    @TableId(value = "id", type = IdType.AUTO)
    private long id;
    @ApiModelProperty(value = "任务ID")
    private String taskId;
    @ApiModelProperty(value = "任务名")
    private String taskName;
    @ApiModelProperty(value = "扩展字段")
    private String feature;
    @ApiModelProperty(value = "任务进度")
    private String progressRate;
    @ApiModelProperty(value = "任务状态")
    private String taskType;
    @ApiModelProperty(value = "最大重试次数")
    private Integer maxRetry;
    @ApiModelProperty(value = "任务提交者")
    private String modifier;
    @ApiModelProperty(value = "创建时间")
    private Date gmtCreate;
    @ApiModelProperty(value = "修改时间")
    private Date gmtModified;

    @Override
    protected void resetFeature() {
        feature = JSONObject.toJSONString(getFeatureMap());
    }

    @Override
    protected String getFeatureString() {
        return feature;
    }

    public static Task buildTask(String taskId, String taskName, Map<String,String> paramsMap){
        return buildTask(taskId,taskName, TaskTypeEnums.INIT.getCode(),paramsMap,"SYS","100",0);
    }
    public static Task buildTask(String taskId, String taskName, String taskType, Map<String,String> paramsMap,Integer maxRetry){
        return buildTask(taskId,taskName,taskType,paramsMap,"SYS","100",maxRetry);
    }

    public static Task buildTask(String taskId,String taskName,String taskType,Map<String,String> paramsMap,String modifier,String progressRate,Integer maxRetry){
        Task task = new Task();
        task.setTaskId(taskId);
        task.setTaskName(taskName);
        task.setTaskType(taskType);
        task.addFeatureMap(paramsMap);
        task.setModifier(modifier);
        task.setProgressRate(progressRate);
        task.setGmtCreate(new Date());
        task.setGmtModified(new Date());
        task.setMaxRetry(maxRetry);
        return task;
    }
}